package com.morizhang.merchanmanager.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.ApiResult;
import com.morizhang.merchanmanager.api.IService;
import com.morizhang.merchanmanager.api.model.CommoditySellingModel;
import com.morizhang.merchanmanager.api.plugins.BasicSaleManage;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;

@ServiceAnnotation(name = "DoSaleService")
public class DoSaleService implements IService {
    @Autowired
    BasicSaleManage basicSaleManage;

    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        String callMethodName = requestParams.getString("requestMethod");
        Method callMethod = this.getClass().getMethod(callMethodName, JSONObject.class);
        ApiResult result = (ApiResult)callMethod.invoke(this, requestParams);
        result.setSuccess(true);
        return result;
    }

    public ApiResult checkSelling(JSONObject requestParams) {
        String barCode = requestParams.getJSONObject("data").getString("barCode");
        JSONObject resultJson = new JSONObject();
        resultJson.put("result", basicSaleManage.checkSelling(barCode));
        ApiResult result = new ApiResult();
        result.setData(resultJson);
        return result;
    }

    public ApiResult doOneSale(JSONObject requestParams) {
        JSONArray saleList = requestParams.getJSONArray("data");
        basicSaleManage.doSale(saleList);
        return new ApiResult();
    }
}
